package com.example.springboot.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Description: MySpringTask
 * @Author wenlinyu
 * @Date: 2020/12/16 10:14
 * @Version 1.0
 */
@Component
public class MySpringTask {

    /**
     * 每2秒执行1次
     * @throws InterruptedException
     */
    //@Scheduled(fixedRate = 2000)
    public void fixedRateMethod() throws InterruptedException {
        System.out.println("fixedRateMethod: " + new Date());
        Thread.sleep(1000);
    }

    /**
     * 执行结束2秒后执行下次任务
     * @throws InterruptedException
     */
    //@Scheduled(fixedDelay = 2000)
    public void fixedDelayMethod() throws InterruptedException {
        System.out.println("fixedDelayMethod: " + new Date());
        // 每次打印完，先等待1秒，然后方法执行结束2秒后再次执行任务，所以是每3秒打印一次
        Thread.sleep(1000);
    }

    /**
     *
     * @throws InterruptedException
     */
    //@Scheduled(cron = "0/2 * * * * ?")
    public void jump() throws InterruptedException {
        System.out.println("心跳检测：" + new Date());
    }
}
